package view.manager;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import base.NumberGeneration;
import controller.DateChooser;
import dao.ClassinformationDao;
import dao.MajorDao;
import dao.StudentDao;
import model.Classinformation;
import model.Major;
import model.Student;
import util.StringUtil;

import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;

public class StudentAddInterfrm extends JInternalFrame {
	private JTextField textFieldId;
	private JTextField textFieldName;
	private JTextField textFieldSfz;
	private JTextField textFieldTel;
	private JTextField textFieldProvince;
	private JTextField textFieldPass;
	private JTextField textFieldNation;
	private JTextField textFieldMail;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JComboBox comboBoxPolitical;
	private JComboBox comboBoxMajor;
	private JComboBox comboBoxClass;
	private JPanel panelDtae;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentAddInterfrm frame = new StudentAddInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StudentAddInterfrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("学生添加");
		setBounds(100, 100, 450, 607);

		JLabel lblNewLabel = new JLabel("学生学号：");
		lblNewLabel.setBounds(105, 59, 84, 20);
		lblNewLabel.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/num.png")));

		textFieldId = new JTextField();
		textFieldId.setBounds(199, 59, 136, 21);
		textFieldId.setEditable(false);
		textFieldId.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("学生姓名：");
		lblNewLabel_1.setBounds(105, 119, 84, 20);
		lblNewLabel_1.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/username.png")));

		textFieldName = new JTextField();
		textFieldName.setBounds(200, 117, 132, 21);
		textFieldName.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("学生性别：");
		lblNewLabel_3.setBounds(105, 149, 84, 20);
		lblNewLabel_3.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/性别.png")));

		JRadioButton rdbtnNewRadioButtonMan = new JRadioButton("男");
		rdbtnNewRadioButtonMan.setBounds(210, 148, 37, 23);
		buttonGroup.add(rdbtnNewRadioButtonMan);
		rdbtnNewRadioButtonMan.setSelected(true);
		JRadioButton rdbtnNewRadioButtonWoman = new JRadioButton("女");
		rdbtnNewRadioButtonWoman.setBounds(276, 148, 37, 23);
		buttonGroup.add(rdbtnNewRadioButtonWoman);

		JLabel lblNewLabel_4 = new JLabel("出生日期：");
		lblNewLabel_4.setBounds(105, 191, 84, 20);
		lblNewLabel_4.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/出生日期.png")));

		panelDtae = new JPanel();
		panelDtae.setBounds(199, 179, 130, 35);
		final DateChooser mp = new DateChooser();
		panelDtae.add(mp, BorderLayout.CENTER);

		JLabel lblNewLabel_6 = new JLabel("身份证号：");
		lblNewLabel_6.setBounds(106, 232, 84, 20);
		lblNewLabel_6.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/身份证.png")));

		textFieldSfz = new JTextField();
		textFieldSfz.setBounds(200, 232, 130, 21);
		textFieldSfz.setColumns(10);

		JLabel lblNewLabel_7 = new JLabel("电话号码：");
		lblNewLabel_7.setBounds(106, 266, 84, 20);
		lblNewLabel_7.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/电 话.png")));

		textFieldTel = new JTextField();
		textFieldTel.setBounds(199, 266, 131, 21);
		textFieldTel.setColumns(10);

		JLabel lblNewLabel_8 = new JLabel("籍贯：");
		lblNewLabel_8.setBounds(105, 302, 60, 20);
		lblNewLabel_8.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/户籍办理.png")));

		textFieldProvince = new JTextField();
		textFieldProvince.setBounds(199, 302, 130, 21);
		textFieldProvince.setColumns(10);

		JLabel lblNewLabel_9 = new JLabel("登录密码：");
		lblNewLabel_9.setBounds(105, 332, 84, 20);
		lblNewLabel_9.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/密码.png")));

		textFieldPass = new JTextField();
		textFieldPass.setBounds(199, 332, 130, 21);
		textFieldPass.setColumns(10);

		JLabel lblNewLabel_10 = new JLabel("民族：");
		lblNewLabel_10.setBounds(105, 362, 60, 20);
		lblNewLabel_10.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/民族.png")));

		textFieldNation = new JTextField();
		textFieldNation.setBounds(199, 362, 130, 21);
		textFieldNation.setColumns(10);

		JLabel lblNewLabel_11 = new JLabel("政治面貌：");
		lblNewLabel_11.setBounds(105, 402, 84, 20);
		lblNewLabel_11.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/政治.png")));

		JLabel lblNewLabel_12 = new JLabel("邮箱：");
		lblNewLabel_12.setBounds(105, 444, 60, 20);
		lblNewLabel_12.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/邮箱.png")));

		textFieldMail = new JTextField();
		textFieldMail.setBounds(199, 444, 130, 21);
		textFieldMail.setColumns(10);

		JLabel lblNewLabel_13 = new JLabel("所在系：");
		lblNewLabel_13.setBounds(107, 26, 72, 20);
		lblNewLabel_13.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/describe.png")));

		comboBoxMajor = new JComboBox();
		comboBoxMajor.setBounds(199, 26, 136, 21);
		comboBoxMajor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBoxMajor.getSelectedItem() instanceof Major) {
					textFieldId.setText(NumberGeneration.createStudentNum((Major) comboBoxMajor.getSelectedItem()));
					comboBoxClass.removeAllItems();
					fillClassType((Major) comboBoxMajor.getSelectedItem());
				} else {
					textFieldId.setText("");
				}
			}
		});

		JButton btnNewButton = new JButton("添加");
		btnNewButton.setBounds(122, 504, 96, 29);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String stu_id, stu_name, stu_sex = "", stu_birthday, stu_sfz, stu_tel, stu_province, stu_email,
						password, nation, political = "", m_id = null, cla_id = null;
				stu_id = textFieldId.getText();
				stu_name = textFieldName.getText();
//				stu_sex = textFieldMajor.getText();
//				stu_birthday = textFieldMajor.getText();
				stu_sfz = textFieldSfz.getText();
				stu_tel = textFieldTel.getText();
				stu_province = textFieldProvince.getText();
				stu_email = textFieldMail.getText();
				password = textFieldPass.getText();
				nation = textFieldNation.getText();
//				political = textFieldPass.getText();
//				m_id = textField_2.getText();
				if (StringUtil.isEmpty(stu_id)) {
				JOptionPane.showMessageDialog(null, "请选择专业", "提示", JOptionPane.ERROR_MESSAGE);
				return;
			}
				if (rdbtnNewRadioButtonWoman.isSelected()) {
					stu_sex = "女";
				}
				if (rdbtnNewRadioButtonMan.isSelected()) {
					stu_sex = "男";
				}
				if (comboBoxMajor.getSelectedItem() instanceof Major) {
					Major major = (Major) comboBoxMajor.getSelectedItem();
					m_id = major.getM_id();
				}
				if (comboBoxClass.getSelectedItem() instanceof Classinformation) {
					Classinformation classinformation = (Classinformation) comboBoxClass.getSelectedItem();
					cla_id = classinformation.getCla_id();
				}
				if (!"请选择政治面貌".equals(comboBoxPolitical.getSelectedItem().toString())) {
					political = comboBoxPolitical.getSelectedItem().toString();
				}
				stu_birthday = mp.getSelectDate();
				if (StringUtil.isEmpty(stu_id)) {
					JOptionPane.showMessageDialog(null, "请选择专业与班级", "提示", JOptionPane.ERROR_MESSAGE);
					textFieldId.requestFocus();
					return;
				}
//				if (!StringUtil.isNum(stu_id)) {
//					JOptionPane.showMessageDialog(null, "学生编号需为数字", "提示", JOptionPane.ERROR_MESSAGE);
//					textFieldId.requestFocus();
//					textFieldId.selectAll();
//					return;
//				}
//				if (stu_id.length() >= 11) {
//					JOptionPane.showMessageDialog(null, "学生编号太长", "提示", JOptionPane.ERROR_MESSAGE);
//					textFieldId.requestFocus();
//					textFieldId.selectAll();
//					return;
//				}
//				for (Student studentName : student) {
//					if (stu_id.equals(studentName.getStu_id())) {
//						JOptionPane.showMessageDialog(null, "学生编号已存在", "提示", JOptionPane.ERROR_MESSAGE);
//						textFieldId.requestFocus();
//						textFieldId.selectAll();
//						return;
//					}
//				}

				if (StringUtil.isEmpty(stu_name)) {
					JOptionPane.showMessageDialog(null, "学生姓名不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textFieldName.requestFocus();
					return;
				}
				if (!StringUtil.isEmpty(stu_sfz) && !StringUtil.isIDCard(stu_sfz)) {
					JOptionPane.showMessageDialog(null, "身份证号错误", "提示", JOptionPane.ERROR_MESSAGE);
					textFieldSfz.requestFocus();
					textFieldSfz.selectAll();
					return;
				}
				if (!StringUtil.isEmpty(stu_tel) && !StringUtil.isMobile(stu_tel)) {
					JOptionPane.showMessageDialog(null, "电话格式错误", "提示", JOptionPane.ERROR_MESSAGE);
					textFieldTel.requestFocus();
					textFieldTel.selectAll();
					return;
				}
				if (!StringUtil.isEmpty(password) && !StringUtil.checkPasswordRule(password)) {
					JOptionPane.showMessageDialog(null, "密码长度大于8位小于30位，包含3种字符", "提示", JOptionPane.ERROR_MESSAGE);
					textFieldPass.requestFocus();
					textFieldPass.selectAll();
					return;
				} else {
					password = "111";
				}

				if (!StringUtil.isEmpty(stu_email) && !StringUtil.isEmail(stu_email)) {
					JOptionPane.showMessageDialog(null, "邮箱格式错误", "提示", JOptionPane.ERROR_MESSAGE);
					textFieldMail.requestFocus();
					textFieldMail.selectAll();
					return;
				}
				Calendar cal = Calendar.getInstance();
				int enrollmentYear = cal.get(Calendar.YEAR);
				Student s = new Student(stu_id, stu_name, stu_sex, stu_birthday, stu_sfz, stu_tel, stu_province,
						stu_email, password, nation, political, m_id,cla_id,enrollmentYear);
				if (StudentDao.insert(s) > 0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					comboBoxMajor.setSelectedIndex(0);
					comboBoxPolitical.setSelectedIndex(0);
					textFieldName.setText("");
					textFieldMail.setText("");
					textFieldSfz.setText("");
					textFieldTel.setText("");
					textFieldProvince.setText("");
					textFieldPass.setText("");
					textFieldNation.setText("");
					rdbtnNewRadioButtonMan.setSelected(true);
					mp.setSelectDate();
				} else {
					JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/add.png")));

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.setBounds(227, 504, 96, 29);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textFieldName.setText("");
				textFieldMail.setText("");
				textFieldSfz.setText("");
				textFieldTel.setText("");
				textFieldProvince.setText("");
				textFieldPass.setText("");
				textFieldNation.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/resets.png")));

		comboBoxPolitical = new JComboBox();
		comboBoxPolitical.setBounds(200, 401, 129, 23);
		
		JLabel lblNewLabel_2 = new JLabel("班级：");
		lblNewLabel_2.setBounds(105, 89, 60, 20);
		lblNewLabel_2.setIcon(new ImageIcon(StudentAddInterfrm.class.getResource("/images/班级.png")));
		
		comboBoxClass = new JComboBox();
		comboBoxClass.setBounds(199, 88, 135, 23);
		getContentPane().setLayout(null);
		getContentPane().add(btnNewButton);
		getContentPane().add(btnNewButton_1);
		getContentPane().add(lblNewLabel_4);
		getContentPane().add(panelDtae);
		getContentPane().add(lblNewLabel_3);
		getContentPane().add(rdbtnNewRadioButtonMan);
		getContentPane().add(rdbtnNewRadioButtonWoman);
		getContentPane().add(lblNewLabel_13);
		getContentPane().add(comboBoxMajor);
		getContentPane().add(lblNewLabel_2);
		getContentPane().add(lblNewLabel);
		getContentPane().add(comboBoxClass);
		getContentPane().add(textFieldId);
		getContentPane().add(lblNewLabel_1);
		getContentPane().add(textFieldName);
		getContentPane().add(lblNewLabel_12);
		getContentPane().add(textFieldMail);
		getContentPane().add(lblNewLabel_11);
		getContentPane().add(comboBoxPolitical);
		getContentPane().add(lblNewLabel_9);
		getContentPane().add(textFieldPass);
		getContentPane().add(lblNewLabel_8);
		getContentPane().add(lblNewLabel_10);
		getContentPane().add(textFieldNation);
		getContentPane().add(textFieldProvince);
		getContentPane().add(lblNewLabel_7);
		getContentPane().add(textFieldTel);
		getContentPane().add(lblNewLabel_6);
		getContentPane().add(textFieldSfz);
		fillPoliticalType();
		fillMajorType();

	}

	/**
	 * 初始化下拉框 * 中共党员、中共预备党员、共青团员、民革党员、民盟盟员、民建会员、 民进会员、农工党党员、致公党党员
	 * 、九三学社社员、台盟盟员、无党派人士、群众
	 */
	private void fillPoliticalType() {
		String[] politicalArr = { "请选择政治面貌", "中共党员", "中共预备党员", "共青团员", "民革党员", "民盟盟员", "民建会员", "民进会员", "农工党党员", "致公党党员",
				"九三学社社员", "台盟盟员", "无党派人士", "群众" };
		for (String string : politicalArr) {
			comboBoxPolitical.addItem(string);
		}
	}

	/**
	 * 初始化下拉框
	 */
	private void fillMajorType() {
		comboBoxMajor.addItem("请选择专业");
		ArrayList<Major> arrayList = MajorDao.select();
		for (Major major : arrayList) {
			comboBoxMajor.addItem(major);
		}
	}
	/**
	 * 初始化下拉框
	 */
	private void fillClassType(Major major) {
		comboBoxClass.addItem("请选择班级");
		if (comboBoxMajor.getSelectedItem() instanceof Major) {
			ArrayList<Classinformation> arrayList = ClassinformationDao.select();
			for (Classinformation classinformation : arrayList) {
				if (classinformation.getM_id().equals(major.getM_id())) {
					comboBoxClass.addItem(classinformation);
				}
			}
		}
	}
}
